<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use app\index\model\Cate as CateModel;
use app\index\model\Article as AricleModel;

class Cate extends Typez
{
    public function index($type){
//        if ($type == ' '){
//            return '1';
//        }
        $article = new AricleModel();
        if(!empty(input('cid'))){
            //传递id显示对应文章
            $cid = input('cid');
            $cateart = $article->get_cate_art($cid);
            if ($cateart){
                $catename = $cateart[0]['catename'];
                $cateart_qi = $article->get_cate_art_qi($cid);
            }else{
//                return view('cate/cate');
                $catename = '给分类下暂时没有文章，敬请期待！';
                $cateart_qi = $article->getarticle_qi($type);
            }

        }else{
            if(!empty(input('keywords'))){
                // 点击关键词搜索
                // $article = new AricleModel();
                $keywords=input('keywords');
                $data['a.keywords'] = array('like', "%$keywords%");
                $data['a.typez'] = $type;
//                $datac['a.content'] = array('like', "%$keywords%");
                $cateart=db('article')->where($data)->field('a.*,b.catename')->alias('a')->join('lan_cate b','a.cateid=b.id')->select();
                if(empty($cateart)){
                    // 未搜索到相关内容
                    // $article = new AricleModel();
                    $cateart = $article->getarticle_tui($type);
                    $catename = '未搜索到相关文章,为您推荐最新:';
                }else{
                    $catename = '搜索结果';
                }
                $cateart_qi = $article->getarticle_qi($type);
            }else{
                // 未传id显示最新文章
                // $article = new AricleModel();
                $cateart = $article->getarticle_tui($type);
                $catename = '最新文章';
                $cateart_qi = $article->getarticle_qi($type);
            }
           
        }
        $cate = new CateModel();
        $cateres = $cate->getcate($type);
        $ty = $type;
        $this->assign(array(
            'cateres' => $cateres,
            'cateart' => $cateart,
            'catename' => $catename,
            'cateart_qi' => $cateart_qi,
            'ty' => $type
        ));
        return view('cate/cate');
    }

    public function tz(){
        $article = new AricleModel();
        if(!empty(input('cid'))){
            //传递id显示对应文章
            $cid = input('cid');
            // $article = new AricleModel();
            $cateart = $article->get_cate_art($cid);
//            return dump($cid);
            $catename = $cateart[0]['catename'];
            $cateart_qi = $article->get_cate_art_qi($cid);
            // return dump($cateart[0]['catename']);
            // die;
        }else{
            if(!empty(input('keywords'))){
                // 点击关键词搜索
                // $article = new AricleModel();
                $keywords=input('keywords');
                $data['a.keywords'] = array('like', "%$keywords%");
//                $data['a.typez'] = $type;
//                $data['a.content'] = array('like', "%$keywords%");
                $cateart=db('article')->where($data)->field('a.*,b.catename')->alias('a')->join('lan_cate b','a.cateid=b.id')->select();
                if(empty($cateart)){
                    // 未搜索到相关内容
                    // $article = new AricleModel();
                    $cateart = $article->getarticle_tui(2);
                    $catename = '未搜索到相关文章,为您推荐最新:';
                }else{
                    $catename = '搜索结果';
                }
                $cateart_qi = $article->getarticle_qi(2);
            }else{
                // 未传id显示最新文章
                // $article = new AricleModel();
                $cateart = $article->getarticle_tui(2);
                $catename = '最新文章';
                $cateart_qi = $article->getarticle_qi(2);
            }

        }
        $cate = new CateModel();
        $cateres = $cate->getcate(2);
        $this->assign(array(
            'cateres' => $cateres,
            'cateart' => $cateart,
            'catename' => $catename,
            'cateart_qi' => $cateart_qi
        ));
        return view('cate/cate');
    }

    public function pic_cate(){
        $data = db('article')->where('thumb' , '>', ' ')->select();
        $this->assign('pic_cate', $data);
        return view();
    }

}
